package org.jiuling.wecom.school.model;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.jiuling.local.tools.exception.LocalToolsException;
import org.jiuling.local.tools.util.EnumUtil;
import org.jiuling.local.tools.verify.ObjectVerify;
import org.jiuling.wecom.school.enums.DepartmentAdminTypeEnum;
import org.jiuling.wecom.school.enums.DivisionalTypeEnum;

import java.util.ArrayList;
import java.util.List;

/**
 * 部门模型
 * @author 紫灵
 * @date 2023年04月03日 14时04分10秒
 */
public class DivisionalModel {

    /** 部门id */
    private long id;
    /** 部门名称 */
    private String name;
    /** 部门类型 */
    private DivisionalTypeEnum type;
    /** 部门管理员 */
    private List<DepartmentAdminModel> departmentAdminModelList;
    /** 父级id */
    private long parentId;

    public long getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public List<DepartmentAdminModel> getDepartmentAdminModelList() {
        return departmentAdminModelList;
    }

    public DivisionalTypeEnum getType() {
        return type;
    }

    public long getParentId() {
        return parentId;
    }

    /** 部门管理员 */
    public static class DepartmentAdminModel{
        /** 用户id */
        private String userId;
        /** 部门管理员类型 */
        private DepartmentAdminTypeEnum type;
        /** 科目 */
        private String subject;

        public String getUserId() {
            return userId;
        }

        public DepartmentAdminTypeEnum getType() {
            return type;
        }

        public String getSubject() {
            return subject;
        }

        /**
         * 建造者
         * @return 建造者
         */
        public static Builder builder(){
            return new Builder();
        }

        /**
         * 建造者
         */
        public static class Builder{

            /** 部门管理模型 */
            private final DepartmentAdminModel departmentAdminModel;

            public Builder() {
                this.departmentAdminModel = new DepartmentAdminModel();
            }

            /**
             * 添加用户id
             * @param userId 用户id
             * @return 建造者
             */
            public Builder addUserId(String userId){
                departmentAdminModel.userId = userId;
                return this;
            }

            /**
             * 添加类型
             * @param type 类型
             * @return 建造者
             */
            public Builder addType(int type){
                try {
                    departmentAdminModel.type = EnumUtil.valueOfEnum(DepartmentAdminTypeEnum.class,type,"getCode");
                } catch (LocalToolsException e) {
                    throw new RuntimeException(e);
                }
                return this;
            }

            /**
             * 添加科目
             * @param subject 科目
             * @return 建造者
             */
            public Builder addSubject(String subject){
                departmentAdminModel.subject = subject;
                return this;
            }

            public Builder addDepartmentAdminByJson(JSONObject departmentAdminJson){
                departmentAdminModel.userId = departmentAdminJson.getString("userid");
                departmentAdminModel.subject = departmentAdminJson.getString("subject");
                try {
                    departmentAdminModel.type = EnumUtil.valueOfEnum(DepartmentAdminTypeEnum.class,departmentAdminJson.getInteger("type"),"getCode");
                } catch (LocalToolsException e) {
                    throw new RuntimeException(e);
                }
                return this;
            }

            /**
             * 建造
             * @return 部门管理员模型
             */
            public DepartmentAdminModel build(){
                return departmentAdminModel;
            }


        }
    }

    /**
     * 建造者
     * @return 建造者
     */
    public static Builder builder(){
        return new Builder();
    }

    /**
     * 建造者
     */
    public static class Builder{
        /** 部门模型 */
        private final DivisionalModel divisionalModel;
        public Builder(){
            this.divisionalModel = new DivisionalModel();
        }

        /**
         * 添加id
         * @param id 部门id
         * @return 建造者
         */
        public Builder addId(long id){
            divisionalModel.id = id;
            return this;
        }

        /**
         * 添加名称
         * @param name 部门名称
         * @return 建造者
         */
        public Builder addName(String name){
            divisionalModel.name = name;
            return this;
        }

        /**
         * 添加类型
         * @param type 部门类型
         * @return 建造者
         */
        public Builder addType(int type){
            try {
                divisionalModel.type = EnumUtil.valueOfEnum(DivisionalTypeEnum.class,type,"getCode");
            } catch (LocalToolsException e) {
                throw new RuntimeException(e);
            }
            return this;
        }

        /**
         * 添加部门管理员
         * @param departmentAdminModelList 部门管理员
         * @return 建造者
         */
        public Builder addDepartmentAdminModelList(List<DepartmentAdminModel> departmentAdminModelList){
            divisionalModel.departmentAdminModelList = departmentAdminModelList;
            return this;
        }

        /**
         * 添加父级id
         * @param parentId 部门父级id
         * @return 建造者
         */
        public Builder addParentid(long parentId){
            divisionalModel.parentId = parentId;
            return this;
        }

        /**
         * 通过JSON添加部门
         * @param divisionalJson 部门Json
         * @return 建造者
         */
        public Builder addDivisionalJson(JSONObject divisionalJson){
            divisionalModel.id = divisionalJson.getLong("id");
            divisionalModel.name = divisionalJson.getString("name");
            divisionalModel.parentId = divisionalJson.getLong("parentid");
            try {
                divisionalModel.type = EnumUtil.valueOfEnum(DivisionalTypeEnum.class,divisionalJson.getInteger("type"),"getCode");
            } catch (LocalToolsException e) {
                throw new RuntimeException(e);
            }
            // 部门管理员
            JSONArray departmentAdminJsonArray = divisionalJson.getJSONArray("department_admins");
            if(!ObjectVerify.isNull(departmentAdminJsonArray) && !departmentAdminJsonArray.isEmpty()){
                List<DepartmentAdminModel> departmentAdminModelList = new ArrayList<>();
                for (int i = 0; i < departmentAdminJsonArray.size(); i++) {
                    departmentAdminModelList.add(DepartmentAdminModel.builder().addDepartmentAdminByJson(departmentAdminJsonArray.getJSONObject(i)).build());
                }
                divisionalModel.departmentAdminModelList = departmentAdminModelList;
            }

            return this;
        }

        /**
         * 建造
         * @return 部门模型
         */
        public DivisionalModel build(){
            return divisionalModel;
        }

    }
}
